<template>
    <div ref="brandRef" class="brand-container" :class="{ 'is-loading': isImageLoading }"
        :style="{ backgroundImage: backgroundImage ? `url(${backgroundImage})` : 'none' }">
        <div class="brand-mask"></div>
        <div class="brand">
            <p class="artboard" :data-text="blogTitle">{{ blogTitle }}</p>
            <div class="title-container">
                <div class="title">
                    <span class="typed-text">{{ typedText }}</span>
                    <span class="easy-typed-cursor">|</span>
                </div>
            </div>
        </div>

        <div class="switch-background-btn" @click="handleSwitchBackground">
            <i class="fas fa-random"></i>
            <span class="hover-tip">切换背景</span>
        </div>

        <Waves class="hero-waves" />
        <i class="fas fa-chevron-down arrow-down" @click="scrollDown"></i>
    </div>
</template>

<script>
import Waves from "@/components/Waves/index.vue";
import axios from "axios";

export default {
    name: "Hero",
    components: {
        Waves,
    },
    data() {
        return {
            blogTitle: "KINGCOLA-ICG BLOG",
            backgroundImage: "https://images.pexels.com/photos/2387793/pexels-photo-2387793.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=1",
            isImageLoading: false,
            typedText: "",
            textsToType: [
                "始于代码，不止于代码。",
                "KingCola-ICG 等待你的加入。",
                "代码，是创造的起点。",
                "每一行，都是热爱。",
                "探索技术，无止境。",
                "分享知识，共成长。",
                "从零到一，见证改变。",
                "保持好奇，迎接变化。",
                "技术塑未来，思想引方向。",
                "记录灵感，点亮思维。",
                "热爱编码，乐在其中。",
                "让代码有温度、有故事。",
                "用代码构建，用思想连接。",
                "每一次提交，都是新的进步。",
                "化繁为简，代码的艺术。",
                "在数字世界里，寻找诗意。",
                "Bug是暂时的，热爱是永恒的。",
                "敲下的是代码，实现的是梦想。",
                "沉迷于逻辑，折服于优雅。",
                "编码人生，无限可能。",
                "思想照进现实。",
                "拥抱变化，不惧挑战。",
                "以匠心，写代码。",
                "解构世界，重构梦想。",
                "程序如诗，优雅永恒。",
                "键盘敲出，未来蓝图。",
                "代码如诗，简约不简单。",
                "创新思维，定义未来。",
                "技术改变，生活之美。",
                "编码之美，在于简约。",
                "写代码，也写人生。",
                "思维碰撞，创意迸发。",
                "逻辑之美，尽在代码中。",
                "专注当下，开创未来。",
                "技术向善，匠心向美。",
                "编程之道，始于热爱。",
                "问题是前进的阶梯。",
                "码出精彩，码出人生。",
                "行云流水，一气呵成。",
                "算法之美，逻辑之趣。",
                "精益求精，止于至善。",
            ],

            currentTextToType: "",
            typingSpeed: 120,
            typingInterval: null,
            imageInterval: null,
            textInterval: null,
            charIndex: 0,
            deleting: false,
            delayCounter: 0,
            titleHovered: false,
        };
    },
    methods: {
        handleSwitchBackground() {
            this.fetchImage(); // 调用现有的fetchImage方法切换背景
        },
        fetchImage() {
            this.isImageLoading = true;

            const loadImage = (url) => {
                const img = new Image();
                img.src = url;
                img.onload = () => {
                    setTimeout(() => {
                        this.backgroundImage = url;
                        this.isImageLoading = false;
                    }, 300);
                };
                img.onerror = () => {
                    // 图片URL本身加载失败，静默处理，仅停止加载动画
                    this.isImageLoading = false;
                };
            };

            const imageSources = ['fj']; // 多种图片获取源
            const randomSource = imageSources[Math.floor(Math.random() * imageSources.length)];
            const apiUrl = `${import.meta.env.VITE_APP_BASE_API}/api/image/${randomSource}`;

            axios
                .get(apiUrl)
                .then((response) => {
                    let imageUrl = null;
                    // 后端接口返回 Result 对象，图片URL在 data.data 中
                    if (response.data && response.data.code === 200 && response.data.data) {
                        if (typeof response.data.data === 'string' && response.data.data.startsWith('http')) {
                            imageUrl = response.data.data;
                        }
                    }

                    if (imageUrl) {
                        loadImage(imageUrl);
                    } else {
                        // 未能从API获取有效URL，静默处理
                        this.isImageLoading = false;
                    }
                })
                .catch(() => {
                    // Axios请求失败，静默处理
                    this.isImageLoading = false;
                });
        },
        startTyping() {
            if (this.deleting) {
                if (this.typedText.length > 0) {
                    this.typedText = this.typedText.substring(0, this.typedText.length - 1);
                    this.typingInterval = setTimeout(this.startTyping, 50);
                } else {
                    this.deleting = false;
                    this.selectNewText();
                    this.typingInterval = setTimeout(this.startTyping, 500);
                }
            } else {
                if (this.charIndex < this.currentTextToType.length) {
                    this.typedText += this.currentTextToType.charAt(this.charIndex);
                    this.charIndex++;
                    const randomSpeed = Math.floor(Math.random() * 80) + this.typingSpeed;
                    this.typingInterval = setTimeout(this.startTyping, randomSpeed);
                } else {
                    if (this.delayCounter < 20) {
                        this.delayCounter++;
                        this.typingInterval = setTimeout(this.startTyping, 100);
                    } else {
                        this.delayCounter = 0;
                        this.deleting = true;
                        this.typingInterval = setTimeout(this.startTyping, 1000);
                    }
                }
            }
        },
        selectNewText() {
            const randomIndex = Math.floor(Math.random() * this.textsToType.length);
            this.currentTextToType = this.textsToType[randomIndex];
            this.charIndex = 0;
        },
        resetAndStartTyping() {
            clearTimeout(this.typingInterval);
            this.typedText = "";
            this.charIndex = 0;
            this.deleting = false;
            this.delayCounter = 0;
            this.selectNewText();
            this.startTyping();
        },
        scrollDown() {
            const mainContent = document.querySelector(".home-main-content");
            if (mainContent) {
                mainContent.scrollIntoView({ behavior: "smooth" });
            }
        },
        handleTitleMouseEnter() {
            this.titleHovered = true;
        },
        handleTitleMouseLeave() {
            this.titleHovered = false;
        }
    },
    mounted() {
        this.fetchImage();
        this.resetAndStartTyping();
        this.imageInterval = setInterval(this.fetchImage, 30000);

        // 添加鼠标交互
        const artboard = document.querySelector('.artboard');
        if (artboard) {
            artboard.addEventListener('mouseenter', this.handleTitleMouseEnter);
            artboard.addEventListener('mouseleave', this.handleTitleMouseLeave);
        }
    },
    beforeDestroy() {
        clearTimeout(this.typingInterval);
        clearInterval(this.imageInterval);
        clearInterval(this.textInterval);

        // 移除事件监听器
        const artboard = document.querySelector('.artboard');
        if (artboard) {
            artboard.removeEventListener('mouseenter', this.handleTitleMouseEnter);
            artboard.removeEventListener('mouseleave', this.handleTitleMouseLeave);
        }
    },
};
</script>

<style lang="scss" scoped>
.brand-container {
    height: 100vh;
    position: relative;
    color: #fff;
    background-position: center;
    background-size: cover;
    transition: background-image 1.5s cubic-bezier(0.4, 0, 0.2, 1);
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    text-align: center;
    overflow: hidden;

    &:not(.is-loading) {
        background-size: 110%;
        animation: kenburns 45s ease-in-out infinite alternate;
    }
}

.brand-container.is-loading {
    background: linear-gradient(-60deg,
            #0b0217,
            #0e0524,
            #1d0c3d,
            #3c165a,
            #1d0c3d,
            #0e0524,
            #0b0217);
    background-size: 300% 300%;
    animation: gradient-flow 8s ease-in-out infinite;
}

@keyframes gradient-flow {
    0% {
        background-position: 0% 50%;
    }

    50% {
        background-position: 100% 50%;
    }

    100% {
        background-position: 0% 50%;
    }
}

@keyframes kenburns {
    0% {
        background-size: 100%;
        background-position: 50% 50%;
    }

    100% {
        background-size: 120%;
        background-position: 30% 70%;
    }
}

.brand-mask {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: linear-gradient(to bottom,
            rgba(0, 0, 0, 0.6) 0%,
            rgba(0, 0, 0, 0.4) 30%,
            rgba(0, 0, 0, 0.4) 70%,
            rgba(0, 0, 0, 0.7) 100%);
    z-index: 0;
}

.brand::before {
    content: '';
    position: absolute;
    width: 130%;
    height: 130%;
    top: -15%;
    left: -15%;
    background: radial-gradient(circle, rgba(0, 0, 0, 0.1) 0%, rgba(0, 0, 0, 0.5) 70%);
    border-radius: 50%;
    opacity: 0.8;
    z-index: -1;
}

.brand {
    position: relative;
    z-index: 2;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    padding: 0 20px;
    transform: translateY(-20px);
    animation: float 6s ease-in-out infinite;
    width: auto;
}

@keyframes float {

    0%,
    100% {
        transform: translateY(-20px);
    }

    50% {
        transform: translateY(-30px);
    }
}

.artboard {
    font-family: "Freestyle Script", "Mistral", "fantasy";
    font-size: 5rem;
    line-height: 1.2;
    margin-bottom: 2rem;
    text-shadow: 0 2px 2px rgba(0, 0, 0, 0.6),
        0 4px 6px rgba(0, 0, 0, 0.4),
        0 8px 18px rgba(0, 0, 0, 0.4),
        0 0 8px rgba(255, 255, 255, 0.2);
    font-weight: 700;
    letter-spacing: 1.5px;
    position: relative;
    background: linear-gradient(to right, #ffffff, #e6e6ff, #c9d9ff, #e6e6ff, #ffffff);
    background-size: 200% 100%;
    -webkit-background-clip: text;
    background-clip: text;
    color: transparent;
    animation: shimmer 8s infinite linear;
    padding: 0 15px;
    max-width: 100%;
    overflow: visible;
    transform: scale(0.95) perspective(1000px) rotateX(2deg);
    transform-origin: center;
    white-space: nowrap;
    z-index: 5;
    display: block;
    -webkit-font-smoothing: antialiased;
    will-change: transform, opacity;
    transition: all 0.5s ease;
    cursor: default;
}

.artboard:hover {
    transform: scale(1) perspective(1000px) rotateX(0deg);
    text-shadow: 0 2px 4px rgba(0, 0, 0, 0.7),
        0 6px 12px rgba(0, 0, 0, 0.5),
        0 12px 24px rgba(0, 0, 0, 0.5),
        0 0 12px rgba(255, 255, 255, 0.3);
    letter-spacing: 2px;
}

.artboard:hover::before {
    filter: blur(12px);
    opacity: 0.9;
}

.artboard::before {
    content: "";
    position: absolute;
    top: -10px;
    left: -10px;
    right: -10px;
    bottom: -10px;
    background: linear-gradient(45deg, transparent, rgba(255, 255, 255, 0.3), transparent);
    background-size: 200% 200%;
    animation: light-sweep 5s infinite linear;
    z-index: -1;
    border-radius: 10px;
    filter: blur(8px);
    opacity: 0.8;
}

.artboard::after {
    content: attr(data-text);
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    z-index: -2;
    background: linear-gradient(to right, rgba(255, 255, 255, 0), rgba(255, 255, 255, 0.4), rgba(255, 255, 255, 0));
    -webkit-background-clip: text;
    background-clip: text;
    color: transparent;
    filter: blur(12px);
    opacity: 0.8;
    animation: glow-pulse 3s infinite alternate;
}

@keyframes shimmer {
    0% {
        background-position: 0% 0;
    }

    100% {
        background-position: 200% 0;
    }
}

@keyframes light-sweep {
    0% {
        background-position: -200% -200%;
    }

    100% {
        background-position: 200% 200%;
    }
}

@keyframes glow-pulse {
    0% {
        opacity: 0.3;
        filter: blur(12px);
    }

    100% {
        opacity: 0.7;
        filter: blur(8px) brightness(1.2);
    }
}

.title-container {
    max-width: 90%;
    margin: 0 auto;
    position: relative;
    overflow: hidden;
    padding: 10px 5px;
    background: rgba(0, 0, 0, 0.2);
    border-radius: 8px;
    box-shadow: 0 4px 15px rgba(0, 0, 0, 0.3);
    backdrop-filter: blur(5px);
    -webkit-backdrop-filter: blur(5px);
}

.title {
    font-size: 1.8rem;
    font-weight: 500;
    text-shadow: 0 2px 8px rgba(0, 0, 0, 0.8);
    white-space: nowrap;
    overflow: hidden;
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 2.2rem;
}

.typed-text {
    display: inline-block;
}

.easy-typed-cursor {
    margin-left: 5px;
    font-weight: 500;
    animation: blink 1s infinite;
    color: #fff;
    text-shadow: 0 0 5px rgba(255, 255, 255, 0.8);
}

@keyframes blink {

    0%,
    100% {
        opacity: 1;
    }

    50% {
        opacity: 0;
    }
}

.arrow-down {
    position: absolute;
    bottom: 40px;
    left: 50%;
    transform: translateX(-50%);
    cursor: pointer;
    animation: bounce 2s infinite;
    z-index: 2;
    color: white;
    font-size: 32px;
    filter: drop-shadow(0 2px 5px rgba(0, 0, 0, 0.5));
    transition: all 0.3s ease;
}

.arrow-down:hover {
    color: #e6e6ff;
    filter: drop-shadow(0 0 10px rgba(255, 255, 255, 0.5));
    transform: translateX(-50%) scale(1.2);
}

@keyframes bounce {

    0%,
    20%,
    50%,
    80%,
    100% {
        transform: translate(-50%, 0);
    }

    40% {
        transform: translate(-50%, -20px);
    }

    60% {
        transform: translate(-50%, -10px);
    }
}

@media (max-width: 1024px) {
    .brand-container {
        height: 100vh;

        &:not(.is-loading) {
            animation: kenburns-mobile 45s ease-in-out infinite alternate;
        }
    }

    .artboard {
        font-size: 4.2rem;
        transform: scale(0.9) perspective(1000px) rotateX(2deg);
    }

    .artboard:hover {
        transform: scale(0.95) perspective(1000px) rotateX(0deg);
    }

    .title {
        font-size: 1.6rem;
    }
}

@media (max-width: 768px) {
    .brand-container {
        padding-top: 60px;

        &:not(.is-loading) {
            animation: none;
            background-size: cover;
        }
    }

    .brand {
        margin-top: -40px;
        transform: translateY(0);
        animation: float-mobile 6s ease-in-out infinite;
    }

    @keyframes float-mobile {

        0%,
        100% {
            transform: translateY(0);
        }

        50% {
            transform: translateY(-10px);
        }
    }

    .artboard {
        font-size: 3.5rem;
        margin-bottom: 1.5rem;
        transform: scale(0.85) perspective(1000px) rotateX(2deg);
        padding: 0 10px;
    }

    .artboard:hover {
        transform: scale(0.9) perspective(1000px) rotateX(0deg);
    }

    .title-container {
        max-width: 95%;
    }

    .title {
        font-size: 1.4rem;
        min-height: 1.8rem;
    }

    .hero-waves {
        position: absolute;
        bottom: 0;
        left: 0;
        width: 100%;
        height: 70px;
        z-index: 1;
        display: flex;
        align-items: flex-end;
        pointer-events: none;
    }

    .arrow-down {
        bottom: 30px;
        font-size: 28px;
        z-index: 5;
    }

    .switch-background-btn {
        bottom: 80px; // 避免与波浪重叠
    }
}

@media (max-width: 480px) {
    .artboard {
        font-size: 2.8rem;
        margin-bottom: 1.2rem;
        transform: scale(0.8) perspective(1000px) rotateX(2deg);
        padding: 0 5px;
    }

    .artboard:hover {
        transform: scale(0.85) perspective(1000px) rotateX(0deg);
    }

    .title-container {
        max-width: 95%;
        padding: 8px 4px;
    }

    .title {
        font-size: 1.1rem;
        min-height: 1.4rem;
    }

    .hero-waves {
        height: 60px;
    }

    .arrow-down {
        font-size: 24px;
        bottom: 25px;
    }
}

@keyframes kenburns-mobile {
    0% {
        background-size: 120%;
        background-position: 50% 50%;
    }

    100% {
        background-size: 140%;
        background-position: 40% 60%;
    }
}

// 背景切换按钮样式
.switch-background-btn {
    position: absolute;
    left: 20px;
    bottom: 20px;
    width: 40px;
    height: 40px;
    border-radius: 50%;
    background: rgba(0, 0, 0, 0.3);
    backdrop-filter: blur(5px);
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: all 0.3s ease;
    z-index: 5;

    i {
        color: rgba(255, 255, 255, 0.9);
        font-size: 18px;
        transition: all 0.3s ease;
    }

    &:hover {
        background: rgba(0, 0, 0, 0.5);
        box-shadow: 0 0 10px rgba(0, 0, 0, 0.5);
        transform: scale(1.05);

        i {
            color: white;
            transform: rotate(180deg);
        }

        .hover-tip {
            opacity: 1;
            visibility: visible;
            transform: translateY(0);
        }
    }

    .hover-tip {
        position: absolute;
        left: 50%;
        bottom: 48px;
        transform: translateX(-50%) translateY(10px);
        background: rgba(0, 0, 0, 0.7);
        color: white;
        font-size: 12px;
        padding: 5px 10px;
        border-radius: 5px;
        white-space: nowrap;
        opacity: 0;
        visibility: hidden;
        transition: all 0.3s ease;

        &:after {
            content: '';
            position: absolute;
            top: 100%;
            left: 50%;
            margin-left: -5px;
            border-width: 5px;
            border-style: solid;
            border-color: rgba(0, 0, 0, 0.7) transparent transparent transparent;
        }
    }
}
</style>